﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using lopet.data;
using System.Drawing;

namespace Lopetvisualizer
{
    public static class PrintExtentions
    {
        private const int _multiplier = 10;

        public static System.Drawing.Point GetDrawingPoint(this lopet.data.Point point) 
        {
            return new System.Drawing.Point
            {
                X = (int)point.X * _multiplier + 250,
                Y = (int)point.Y * _multiplier + 250 
            };
        }

        //public static System.Drawing.Point[] GetCorners(this Cell cell)
        //{
        //    var pointList = new List<System.Drawing.Point>();
        //    var x = cell.Center.X * _multiplier;
        //    var y = cell.Center.Y * _multiplier;
        //    var r = cell.Radius * _multiplier;
            
            
            
        //    //var rdivbyroot3 = r / Math.Sqrt(3);
        //    //var twordivbyroot3 = rdivbyroot3 * 2;

        //    //pointList.Add(new lopet.data.Point { X = x - r, Y = y - rdivbyroot3 }.GetDrawingPoint());
        //    //pointList.Add(new lopet.data.Point { X = x, Y = y - twordivbyroot3 }.GetDrawingPoint());
        //    //pointList.Add(new lopet.data.Point { X = x + r, Y = y - rdivbyroot3 }.GetDrawingPoint());
        //    //pointList.Add(new lopet.data.Point { X = x + r, Y = y + rdivbyroot3}.GetDrawingPoint());
        //    //pointList.Add(new lopet.data.Point { X = x, Y = y + twordivbyroot3 }.GetDrawingPoint());
        //    //pointList.Add(new lopet.data.Point { X = x - r, Y = y + rdivbyroot3 }.GetDrawingPoint());

        //    //return pointList.ToArray();
        //}
        public static void PrintPoint(this lopet.data.Point point, Form form, int size, Color color)
        {
            var pixel = point.GetDrawingPoint();

            if (pixel.X < 0 || pixel.Y < 0)
            {
                return;
            }

            SolidBrush brush = new SolidBrush(color);

            Graphics gr = form.CreateGraphics();

            gr.FillRectangle(brush, new Rectangle(pixel.X, pixel.Y, size, size));
        }

        public static void Print(this Molecule molecule, Form form)
        {
            //Draw Cell
            var pixel = molecule.Point.GetDrawingPoint();

            SolidBrush brush = null;

            if (molecule.Absorbed)
            {
                brush = new SolidBrush(Color.Blue);
            }
            else
            {
                if (molecule.Decomposed)
                {
                    brush = new SolidBrush(Color.Red);
                }
                else
                {
                    brush = new SolidBrush(Color.Black);
                }
            }
            
            Graphics gr = form.CreateGraphics();
            gr.DrawRectangle(new Pen(brush), pixel.X, pixel.Y, 1, 1);
        }

        public static void Print(this Cell cell, Form form)
        {
            //Draw Cell
            var pixel = cell.Center.GetDrawingPoint();
            var radius = cell.Radius * _multiplier;

            SolidBrush brush = new SolidBrush(Color.Black);
            
            Graphics gr = form.CreateGraphics();
            PrintCircle(pixel, radius, form);
        }

        public static void PrintCircle(this System.Drawing.Point point, double radius, Form form)
        {
            point.X = (int) (point.X - radius);
            point.Y = (int) (point.Y - radius);

            if (point.X < 0 || point.Y < 0)
            {
                return;
            }

            Pen pen = new Pen(Color.Black);

            Graphics gr = form.CreateGraphics();

            var tr = 2 * radius;

            gr.DrawEllipse(pen, (float)point.X, (float)point.Y, (float)tr, (float)tr);
        }
    }
}
